﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Media;
using System.Threading;
using ModemGSM;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class FrmIncomingCall : Form
    {
        private bool _IsAnswer = false;
        private TimeSpan _TmAnswer = new TimeSpan(0, 0, 0);
        public FrmIncomingCall()
        {
            InitializeComponent();

        }
        public string NameIncomingCall { get; set; }
        public string NumberIncomingCall { get; set; }

        private void FrmIncomingCall_Shown(object sender, EventArgs e)
        {
            this.LblName.Text = NameIncomingCall;
            this.LblNumber.Text = NumberIncomingCall;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timerBeep.Stop();
            if (!_IsAnswer)
            {
                if (SharedLib.ModemGSM.IsOpen)
                {
                    SharedLib.ModemGSM.KeepAlive();
                    if (SharedLib.ModemGSM.Online)
                    {
                        switch (SharedLib.ModemGSM.ActiveStatus)
                        {
                            case ActiveStatusEnum.Busy:
                            case ActiveStatusEnum.NoDialtone:
                            case ActiveStatusEnum.NoCarrier:
                            case ActiveStatusEnum.NoAnswer:
                                {
                                    SharedLib.ModemGSM.HangUp();
                                    this.Close();
                                }
                                break;
                        }
                        SharedLib.ModemGSM.ReadActiveStatus();
                        if (SharedLib.ModemGSM.ActiveStatus == ActiveStatusEnum.call)
                        {
                            timerBeep.Start();
                            _IsAnswer = true;
                            BntReceiver.Text = ResourcesText.WordHangUp;
                            BntCancel.Visible = false;
                            return;
                        }
                        if (SharedLib.ModemGSM.ActiveStatus == ActiveStatusEnum.Ringing)
                        {
                            System.Console.Beep();
                            timerBeep.Start();
                            return;
                        }
                        if (SharedLib.ModemGSM.Error == "")
                        {
                            SharedLib.ModemGSM.HangUp();
                            this.Close();
                        }
                    }
                }
                else
                {
                    this.Close();
                }
            }
            else
            {
                LblTimer.Text = _TmAnswer.Hours.ToString().PadLeft(2, '0') + ":" + _TmAnswer.Minutes.ToString().PadLeft(2, '0') + ":" + _TmAnswer.Seconds.ToString().PadLeft(2, '0');
                LblTimer.Visible = true;
                Application.DoEvents();
                _TmAnswer = _TmAnswer.Add(new TimeSpan(0,0,1));
                SharedLib.ModemGSM.ReadActiveStatus();
                if (SharedLib.ModemGSM.ActiveStatus == ActiveStatusEnum.call)
                {
                    timerBeep.Start();
                    return;
                }
                if (SharedLib.ModemGSM.Error == "")
                {
                    SharedLib.ModemGSM.HangUp();
                    this.Close();
                }
            }
            timerBeep.Start();
        }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            timerBeep.Stop();
            SharedLib.ModemGSM.HangUp();
            this.Close();
        }

        private void BntReceiver_Click(object sender, EventArgs e)
        {
            if (_IsAnswer)
            {
                timerBeep.Stop();
                SharedLib.ModemGSM.HangUp();
                this.Close();
            }
            else
            {
                SharedLib.ModemGSM.Answer();
                Thread.Sleep(100);
                SharedLib.ModemGSM.ReadActiveStatus();
                BntReceiver.Text = ResourcesText.WordHangUp;
                BntCancel.Visible = false;
                _IsAnswer = true;
            }
        }
    }
}
